package utils

/*
Copyright 2021-2025 The k8gb Contributors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.

Generated by GoLic, for more details see: https://github.com/AbsaOSS/golic
*/

import (
	"fmt"
	"math/rand"
	"sort"
	"strings"
	"time"

	"github.com/miekg/dns"
)

type DNSServer struct {
	Host string
	Port int
}

type DNSList []DNSServer

//nolint:gosec // we don't need cryptographically secure randomness here
var rnd = rand.New(rand.NewSource(time.Now().UnixNano()))

func (s DNSServer) String() string {
	return fmt.Sprintf("%s:%v", s.Host, s.Port)
}

func (l DNSList) String() string {
	var aux []string
	for _, el := range l {
		aux = append(aux, el.String())
	}
	return strings.Join(aux, ",")
}

// Random picks random DNSServer (roundrobin over dnsservers) and returns !found if DNSList is empty
func (l DNSList) Random() (*DNSServer, bool) {
	if len(l) == 0 {
		return nil, false
	}
	randomIndex := rnd.Intn(len(l))
	return &l[randomIndex], true
}

// Dig returns a list of IP addresses for a given FQDN by using the dns servers from parentZoneDNSServers
// dns servers are tried one by one from the parentZoneDNSServers and if there is a non-error response it is returned and the rest is not tried
func Dig(fqdn string, maxRecursion int, parentZoneDNSServers ...DNSServer) (ips []string, err error) {
	if maxRecursion < 0 {
		return nil, fmt.Errorf("maximum recursion limit reached")
	}
	maxRecursion--

	if len(parentZoneDNSServers) == 0 {
		return nil, fmt.Errorf("empty parentZoneDNSServers, provide at least one")
	}
	if len(fqdn) == 0 {
		return ips, nil
	}

	if !strings.HasSuffix(fqdn, ".") {
		fqdn += "."
	}
	msg := new(dns.Msg)
	msg.SetQuestion(fqdn, dns.TypeA)
	ack, err := Exchange(msg, parentZoneDNSServers)
	if err != nil {
		return nil, fmt.Errorf("dig error: %s", err)
	}
	aRecords := make([]*dns.A, 0)
	cnameRecords := make([]*dns.CNAME, 0)
	for _, a := range ack.Answer {
		switch v := a.(type) {
		case *dns.A:
			ips = append(ips, v.A.String())
			aRecords = append(aRecords, v)
		case *dns.CNAME:
			cnameRecords = append(cnameRecords, v)
		}
	}
	aResolved := func(c *dns.CNAME) bool {
		for _, a := range aRecords {
			if c.Target == a.Hdr.Name {
				return true
			}
		}
		return false
	}
	cResolved := func(c *dns.CNAME) bool {
		for _, cname := range cnameRecords {
			if c.Target == cname.Hdr.Name {
				return true
			}
		}
		return false
	}
	// Check for non-resolved CNAMEs
	for _, cname := range cnameRecords {
		if !aResolved(cname) && !cResolved(cname) {
			cnameIPs, err := Dig(cname.Target, maxRecursion, parentZoneDNSServers...)
			if err != nil {
				return nil, err
			}
			ips = append(ips, cnameIPs...)
		}
	}
	sort.Strings(ips)
	return ips, nil
}

func Exchange(m *dns.Msg, parentZoneDNSServers []DNSServer) (msg *dns.Msg, err error) {
	if len(parentZoneDNSServers) == 0 {
		return nil, fmt.Errorf("empty parentZoneDNSServers, provide at least one")
	}
	for _, ns := range parentZoneDNSServers {
		if ns.Host == "" {
			return nil, fmt.Errorf("empty edgeDNSServer.Host in the list")
		}
		msg, err = dns.Exchange(m, ns.String())
		if err != nil {
			continue
		}
		return
	}
	return nil, fmt.Errorf("exchange error: all dns servers were tried and none of them were able to resolve, err: %s", err)
}
